package com.ricohgag.controller.home;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ricohgag.entity.Goods;
import com.ricohgag.entity.GoodsCatalog;
import com.ricohgag.entity.custom.GoodsTools;
import com.ricohgag.service.GoodsCatalogService;
import com.ricohgag.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author ricohgag
 * @date 2018/9/4 16:41
 */

@Controller
@RequestMapping("/home/goods")
public class HomeGoodsController {
    @Autowired
    private GoodsCatalogService goodsCatalogService;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/index")
    public ModelAndView toIndex(GoodsTools goodsTools) throws Exception{
        ModelAndView mav = new ModelAndView("/home/goods/index");

        List<GoodsCatalog> goodsCatalogList = goodsCatalogService.selectGoodsCatalog();

        Page<Goods> page = new Page<>(goodsTools.getCurrent(), 10);

        IPage<Goods> goodsIPage = goodsService.selectHomeGoodsPage(page, goodsTools);

        mav.addObject("goodsCatalogList", goodsCatalogList);
        mav.addObject("navActive", 1);
        mav.addObject("goodsTools", goodsTools);
        mav.addObject("goodsIPage", goodsIPage);
        mav.addObject("pages", goodsIPage.getPages());

        return mav;
    }

    /**
     * 查看商品详情
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/detail")
    public ModelAndView detail(Integer id) throws Exception {
        ModelAndView mav = new ModelAndView("/home/goods/detail");
        Goods goods = goodsService.selectGoodsOne(id);

        mav.addObject("goods", goods);
        return mav;
    }


}
